// pages/myorder/order.js
var order=require('./model/OrderModel.js')
import SystemInfoUtil from '../../utils/SystemInfoUtil.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _currentPageIndex:0,
    isIOS: false,
    orderList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _isIOS = SystemInfoUtil.platform == SystemInfoUtil.IOS
    this.setData({
      isIOS: _isIOS
    })
    if (_isIOS){
      wx.setNavigationBarTitle({
        title: '已订阅课程',
      })
    }
    this.loadOrderList(this.data._currentPageIndex)
  },
  coverLoadError: function (event) {
    var position = event.currentTarget.dataset.position
    var updateField = 'orderList[' + position +'].snapshots'
    this.setData({
      [updateField]: '../../images/course-cover-default.jpg'
    });
  },
  loadOrderList(pageIndex){
    var that=this
    order.getOrderList(pageIndex,function(list){
      if (list!=null&&list.length>0){
        that.setData({
          orderList: that.data.orderList.concat(list),
          _currentPageIndex:pageIndex
        })
      }
    })
  },
  onItemClick:function(event){
    var position = event.currentTarget.dataset.position
    var obj = this.data.orderList[position]
    if(obj!=null){
      var _url = null
      if (obj.orderGoodsType=='3001'){//视频
        _url = '../video-info/video?videoId=' + obj.orderGoodsId;
        _url += '&title=' + obj.orderTitle;
      } else if (obj.orderGoodsType=='3002'){//课程
        _url='../courseinfo/detail?courseId=' + obj.orderGoodsId + '&title=' + obj.orderTitle
      }
      
      wx.navigateTo({
        url: _url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadOrderList(this.data._currentPageIndex+1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})